package com.heima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.reggie.domain.Setmeal;
import com.heima.reggie.domain.SetmealDish;
import com.heima.reggie.dto.SetmealDto;
import com.heima.reggie.service.SetmealDishService;
import com.heima.reggie.service.SetmealService;
import com.heima.reggie.mapper.SetmealMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
* @author liulanhui
* @description 针对表【setmeal(套餐)】的数据库操作Service实现
* @createDate 2023-01-03 14:00:51
*/
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>
    implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishService setmealDishService;
    @Override
    public List<Setmeal> selectSetmealByCategoryId(Long categoryId) {
        List<Setmeal> setmeals = setmealMapper.selectAllByCategoryId(categoryId);
        return setmeals;
    }

    @Override
    @Transactional
    public void saveSetmealAndDish(SetmealDto setmealDto) {
        this.save(setmealDto);
        Long setmealId = setmealDto.getId();
        String idStr = Long.toString(setmealId);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for(int i = 0; i <setmealDishes.size(); ++i){
            SetmealDish setmealDish = setmealDishes.get(i);
            setmealDish.setSetmealId(idStr);
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public Setmeal selectOneById(Long id) {
        Setmeal setmeal = setmealMapper.selectOneById(id);
        return setmeal;
    }

    @Override
    public void updateStatusByIdIn(Integer status, Long[] id) {
        List<Long> longs = Arrays.asList(id);
        setmealMapper.updateStatusByIdIn(status,longs);
    }

    @Override
    @Transactional
    public void deleteSetmealAndDish(Long[] id) {
        List<Long> longs = Arrays.asList(id);
        this.removeByIds(longs);
        setmealDishService.deleteBySetmealIdIn(id);
    }


}




